package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game11;
import com.huike.game.dao.EnvironmentDao;

import org.json.JSONObject;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity11 extends AppCompatActivity {

    private ImageView mIvLeftImage;
    private TextView mTvLeftText;
    private TextView mTvContentTitle;
    private ImageView mIvRigthImage;
    private TextView mTvRightText;

    private TextView mTvGame10Tem;
    private TextView mTvGame10TemVal;
    private TextView mTvGame10HumVal;
    private TextView mTvGame10LightVal;
    private TextView mTvGame10CqVal;
    private TextView mTvGame10PmVal;
    private TextView mTvGame10WayVal;
    private  RequestQueue queue;
    private  Timer timer;

    private RelativeLayout mRlTem;
    private RelativeLayout mRlHum;
    private RelativeLayout mRlLight;
    private RelativeLayout mRlCq;
    private RelativeLayout mRlPm;
    private RelativeLayout mRlWay;
    private EnvironmentDao dao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game11);

        try {
            dao  = new EnvironmentDao(GameActivity11.this);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        queue  = Volley.newRequestQueue(this);

        initIds();





        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                initData();

            }
        },0,3000);

    }

    private void initData() {
//        请求数据
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Api.api + "environmental", new Response.Listener<JSONObject>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(JSONObject jsonObject) {
//                手动解析
                Log.e("TAG", jsonObject.toString());
//                try {
//                    String tem = jsonObject.getString("data");
//                    JSONObject object = new JSONObject(tem);
//                    String tem1 = object.getString("tem");
//                    System.out.println("温度:" + tem1);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

//                GSON解析
                Gson gson = new Gson();
                Game11 json = gson.fromJson(String.valueOf(jsonObject), Game11.class);
//                Log.e("TAG", String.valueOf(json.getData().getPM()));

                if(json.getData().getTem() > 15 ){
                    mRlTem.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlTem.setBackgroundResource(R.drawable.border_back);
                }

                if(json.getData().getHum() > 50){
                    mRlHum.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlHum.setBackgroundResource(R.drawable.border_back);
                }

                if(json.getData().getLight() > 350 ){
                    mRlLight.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlLight.setBackgroundResource(R.drawable.border_back);

                }

                if(json.getData().getCq2() > 300){
                    mRlCq.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlCq.setBackgroundResource(R.drawable.border_back);

                }

                if(json.getData().getPM() > 300){
                    mRlPm.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlPm.setBackgroundResource(R.drawable.border_back);

                }

                if(json.getData().getWay() >= 2){
                    mRlWay.setBackgroundResource(R.drawable.border_back_waring);
                }else{
                    mRlWay.setBackgroundResource(R.drawable.border_back);
                }

                mTvGame10TemVal.setText(json.getData().getTem() +"");
                mTvGame10HumVal.setText(json.getData().getHum() +"");
                mTvGame10LightVal.setText(json.getData().getLight() +"");
                mTvGame10CqVal.setText(json.getData().getCq2() +"");
                mTvGame10PmVal.setText(json.getData().getPM() +"");
                mTvGame10WayVal.setText(json.getData().getWay() +"");

                Log.e("TAG",new Date().getTime() +"");
//                保存到数据库
                Game11.DataBean bean = new Game11.DataBean(new Date().getTime() +"",json.getData().getTem(),json.getData().getHum(),json.getData().getLight(),json.getData().getCq2(),json.getData().getPM(),json.getData().getWay());
                    Log.e("TAG",bean.toString());
                dao.save(bean);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG",volleyError.getMessage());
            }
        });

        queue.add(request);

    }

    private void initIds() {
        mIvLeftImage = (ImageView) findViewById(R.id.iv_left_image);
        mTvLeftText = (TextView) findViewById(R.id.tv_left_text);
        mTvContentTitle = (TextView) findViewById(R.id.tv_content_title);
        mIvRigthImage = (ImageView) findViewById(R.id.iv_rigth_image);
        mTvRightText = (TextView) findViewById(R.id.tv_right_text);

        mTvGame10Tem = (TextView) findViewById(R.id.tv_game10_tem);
        mTvGame10TemVal = (TextView) findViewById(R.id.tv_game10_temVal);
        mTvGame10HumVal = (TextView) findViewById(R.id.tv_game10_humVal);
        mTvGame10LightVal = (TextView) findViewById(R.id.tv_game10_lightVal);
        mTvGame10CqVal = (TextView) findViewById(R.id.tv_game10_cqVal);
        mTvGame10PmVal = (TextView) findViewById(R.id.tv_game10_pmVal);
        mTvGame10WayVal = (TextView) findViewById(R.id.tv_game10_wayVal);

        mRlTem = (RelativeLayout) findViewById(R.id.rl_tem);
        mRlHum = (RelativeLayout) findViewById(R.id.rl_hum);
        mRlLight = (RelativeLayout) findViewById(R.id.rl_light);
        mRlCq = (RelativeLayout) findViewById(R.id.rl_cq);
        mRlPm = (RelativeLayout) findViewById(R.id.rl_pm);
        mRlWay = (RelativeLayout) findViewById(R.id.rl_way);

        mIvRigthImage.setVisibility(View.GONE);
        mTvRightText.setVisibility(View.GONE);

        mTvContentTitle.setText("环境指标");

        mIvLeftImage.setImageResource(R.drawable.tiao);
        mTvLeftText.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        关闭定时器
        timer.cancel();

    }
}
